package cn.yuan.xiaoyu.testmodule.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;

import cn.yuan.xiaoyu.commonmodule.view.recycleview.YRecycleviewRefreshHeadView;

/**
 * Created by yukuoyuan on 2017/8/26.
 * 这是一个可以进行刷新的父布局,所以需要继承viewgroup
 */
public class RefreshLayout extends ViewGroup {
    /**
     * 滑动的最小距离才可以使控件滑动
     */
    private int mTouchSlop;
    /**
     * 动画时间
     */
    private int mMediumAnimationDuration;
    private YRecycleviewRefreshHeadView refreshHeader;
    private View mTarget; // the target of the gesture
    int mCurrentTargetOffsetTop;

    public RefreshLayout(Context context) {
        this(context, null);
    }

    public RefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        /**
         * 获得可以使控件滑动的最小距离
         */
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
        /**
         * 获得配置的动画时间
         */
        mMediumAnimationDuration = getResources().getInteger(
                android.R.integer.config_mediumAnimTime);
//        如果我们想要重写onDraw，就要调用setWillNotDraw（false）
        setWillNotDraw(false);
//        获得屏幕参数
        final DisplayMetrics metrics = getResources().getDisplayMetrics();
        /**
         * 添加刷新布局
         */
        refreshHeader = new YRecycleviewRefreshHeadView(getContext());
        addView(refreshHeader);


    }


    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        final int width = getMeasuredWidth();
        final int height = getMeasuredHeight();
        if (getChildCount() == 0) {
            return;
        }
        if (mTarget == null) {
            ensureTarget();
        }
        if (mTarget == null) {
            return;
        }
        final View child = mTarget;
        final int childLeft = getPaddingLeft();
        final int childTop = getPaddingTop();
        final int childWidth = width - getPaddingLeft() - getPaddingRight();
        final int childHeight = height - getPaddingTop() - getPaddingBottom();
        child.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);
        int circleWidth = refreshHeader.getMeasuredWidth();
        int circleHeight = refreshHeader.getMeasuredHeight();
        refreshHeader.layout((width / 2 - circleWidth / 2), mCurrentTargetOffsetTop,
                (width / 2 + circleWidth / 2), mCurrentTargetOffsetTop + circleHeight);
    }

    private void ensureTarget() {
        // Don't bother getting the parent height if the parent hasn't been laid
        // out yet.
        if (mTarget == null) {
            for (int i = 0; i < getChildCount(); i++) {
                View child = getChildAt(i);
                if (!child.equals(refreshHeader)) {
                    mTarget = child;
                    break;
                }
            }
        }
    }

}
